/**
 * Copyright (c) 2014 zhenrongbao.com, Inc. All Rights Reserved
 * @author: jiangshuguang
 * @Date  : 14-11-5
 * @Time  : 下午3:30
 * @brief :
 */
(function () {
    var $page = $(".page-assetsiphone6"),
        $construct = $page.find('.summary-block'),
        Dialog = require("common:widget/ui/dialog/dialog.js").Dialog;

    P.createPage({
        _data:{
            pid:0,
            summary: P.data('summary')
        },
        _create:function(){
            var tip = new Dialog({
                'width':450,
                'content':'' +
                    '<div style="line-height: 30px;">' +
                    '<div>系统会自动提现到您指定的银行卡</div>' +
                    '<div>如果未绑定银行卡，请在”已绑定的银行卡“后点击”添加“</div>' +
                    '</div>'
            });
            this.data({'tip':tip});
        },

        //事件
        _init:function(){
            var tip = this.data('tip'),
                me = this;
            $construct.find('.btn-recharge-tip').click(function(){
                tip.show();
            });

            $construct.find('.btn-modifybank,.btn-addbank').click(function(e){
                e.stopPropagation();
                $construct.find('.bank-list').html('正在获取...').show();
                me.showBankList();
            });

            $("body").click(function(){
                $construct.find('.bank-list').hide();
            });
        },

        //绑定银行卡
        showBankList:function(){
            var $bankList = $construct.find('.bank-list') ,
                $bankcardContainer = $construct.find('.bankcard-container'),
                noBankTpl = '您还没有添加银行卡，<a href="/user/withdraw">去添加</a>';

            $.get('/bank/getbankcard',function(ret,status){
                if(ret.error_no == 0){
                    var html = '<ul>',
                        list = ret.data;
                    if(list.length){
                        $.each(list,function(k,v){
                            html+='' +
                                '<li ' +
                                'data-id="'+v['id']+'" ' +
                                'data-bank-number="'+ v["bankcard_number"]+'">'+
                                v['bankcard_number'] +
                                '</li>'
                        });
                        html+='</ul>';
                        $bankList.html(html).show().find('li').click(function(e){
                            var bankId = $(this).attr('data-id'),
                                bankcardNumber = $(this).attr('data-bank-number');
                            $.post("/iActivity/bindbank",{
                                bank_id:bankId
                            },function(ret,status){
                                if(ret.error_no == 0){
                                    $bankcardContainer.find('.bankcard').text(mathUtil.edSubstr(bankcardNumber));
                                    $bankcardContainer.find('.btn-addbank').text('修改');
                                    $bankList.hide();
                                }
                            },"json");
                        });
                    }else{
                        $bankList.html(noBankTpl);
                    }
                }else{
                    $bankList.html('获取银行卡信息失败')
                }
            },"json")
        }
    });
})();